using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DamageShieldEffectCnotroller:MonoBehaviour {
	[SerializeField] Sprite[] sprites;
	[SerializeField] Sprite[] spritesHit;
	[SerializeField] Sprite[] spritesBreaking;
	[SerializeField] float hitEffectTime = 0.1f;

	SpriteRenderer spriteRenderer;
	int index;
	private void Start() {
		spriteRenderer=GetComponent<SpriteRenderer>();
		Mob mob = GetComponentInParent<Mob>();
		Vector2 size = mob.collider.bounds.size;
		if(size.x<=0.4f&&size.y<=0.4f) index=0;
		else if(size.x<=1f&&size.y<=1f) index=1;
		else index=2;

		transform.position=mob.collider.bounds.center+Vector3.up*0.125f;
		spriteRenderer.sprite=sprites[index];
	}
	int lastOperation = 0;
	float timeSinceOperation = 0;
	private void Update() {
		if(lastOperation!=0) timeSinceOperation+=Time.deltaTime;
		if(timeSinceOperation>hitEffectTime) {
			timeSinceOperation=0;
			switch(lastOperation) {
			case 1:
				spriteRenderer.sprite=sprites[index];
				break;
			case 2:
				Destroy(gameObject);
				break;
			}
			lastOperation=0;

		}
	}

	public void OnHit() {
		lastOperation=1;
		timeSinceOperation=0;
		spriteRenderer.sprite=spritesHit[index];
	}
	public void OnBreak() {
		lastOperation=2;
		timeSinceOperation=0;
		spriteRenderer.sprite=spritesBreaking[index];
	}
}
